package cn.zmit.kuxi.holder;

import org.apache.commons.httpclient.methods.GetMethod;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.activity.GoodsDetailActivity;
import cn.zmit.kuxi.activity.Joinrecord;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.entity.AlreadyPublicEntity;
import cn.zmit.kuxi.entity.JoinRecordEntity;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.interfaces.OnListItemSelectListener;

import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.functions.holder.ViewHolderBase;

/** * @author chenxu @parameter * @return */
public class JoinRecordHolder extends ViewHolderBase<JoinRecordEntity> {
	private TextView title, goodsName, state, joinNum, time;
	private ImageView goodsPic;
	private Context context;
	private Joinrecord joinrecord;
	String userid;
	private TextView tv_goods_join;
	private TextView tv_goods_allneed;
	private ProgressBar progressBar_goods;
	private LinearLayout ll_root;
	TextView tv_thistime_join;
	private static OnListItemSelectListener<JoinRecordEntity> onlistItemSelect;
	private TextView joinu;

	@SuppressLint("InflateParams")
	@Override
	public View createView(LayoutInflater layoutInflater) {
		context = layoutInflater.getContext();
		View view = layoutInflater.inflate(R.layout.item_joinrecord, null);
		title = (TextView) view.findViewById(R.id.tv_title);
		goodsName = (TextView) view.findViewById(R.id.tv_goodsName);
		state = (TextView) view.findViewById(R.id.tv_winState);
		joinNum = (TextView) view.findViewById(R.id.tv_joinCount);
		time = (TextView) view.findViewById(R.id.tv_time);
		goodsPic = (ImageView) view.findViewById(R.id.imag_goodsPic);
		progressBar_goods = (ProgressBar) view
				.findViewById(R.id.progressBar_goods);
		tv_goods_allneed = (TextView) view.findViewById(R.id.tv_record_allneed);
		tv_goods_join = (TextView) view.findViewById(R.id.tv_record_join);
		ll_root = (LinearLayout) view.findViewById(R.id.llRoot);
		tv_thistime_join = (TextView) view.findViewById(R.id.tv_thistime_join);

		joinu = (TextView) view.findViewById(R.id.join);
		Boolean hasLog = PreferenceHelper.readBoolean(context,
				SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED,
				false);
		if (hasLog) {
			userid = PreferenceHelper.readString(context,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID);
		}
		return view;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public void showData(final int position, final JoinRecordEntity itemData) {

		if (itemData.getUrl() != null && itemData.getUrl().length() > 0) {
			ImageDisplayer.getInstance().dispalyImageWithRadius(
					Url.IMAGE_PICTURES + itemData.getUrl(), goodsPic, 0);
		} else {
			goodsPic.setImageResource(R.drawable.ic_launcher);
		}
		title.setText("第" + itemData.getTitle() + "期");
		goodsName.setText(itemData.getName());

		// ll_root.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// Log.e("itemclick==", "click...");
		// Intent intent = new Intent();
		// intent.setAction("cn.zmit.kuxi.activity.GoodsDetailActivity");
		// intent.putExtra("gp_id", itemData.getGp_id());
		// context.startActivity(intent);
		// }
		// });
		// tv_thistime_join.setText("本次参与："+);
		ll_root.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onlistItemSelect != null) {
					onlistItemSelect.onlistItemSelect(position, itemData);
				}
			}
		});

		// 揭晓时间为空，未揭晓
		if (itemData.getTime().equals("null")) {
			state.setVisibility(View.GONE);
			time.setVisibility(View.GONE);
			joinNum.setVisibility(View.GONE);
			progressBar_goods.setVisibility(View.VISIBLE);
			tv_goods_allneed.setVisibility(View.VISIBLE);
			tv_goods_join.setVisibility(View.VISIBLE);
			double all = Integer.parseInt(itemData.getAllNeed());
			double join = Integer.parseInt(itemData.getJoinCount());
			final double result = ((join / all) * 100);
			progressBar_goods.setMax(100);
			progressBar_goods.setProgress((int) (result));
			tv_goods_allneed.setText("总需人数：" + itemData.getAllNeed());
			tv_goods_join.setText("已参与：" + itemData.getJoinCount());
			tv_thistime_join.setVisibility(View.VISIBLE);
			tv_thistime_join.setText("本期参与：" + itemData.getMy_buy_count());
			joinu.setVisibility(View.INVISIBLE);
		} else {

			tv_goods_allneed.setVisibility(View.GONE);
			tv_goods_join.setVisibility(View.INVISIBLE);
			progressBar_goods.setVisibility(View.GONE);
			state.setVisibility(View.VISIBLE);
			time.setVisibility(View.VISIBLE);
			joinNum.setVisibility(View.VISIBLE);
			
			int now_time = itemData.getNow_time();
			int unix_time = itemData.getUnix_time();
			if (unix_time - now_time > 0) {
				time.setText("揭晓时间：等待中...");
				state.setText("敬请期待...");
			} else {
				tv_thistime_join.setVisibility(View.GONE);
				time.setText("揭晓时间：" + itemData.getTime());
				joinu.setVisibility(View.VISIBLE);
				if (itemData.getLucky_num().equals("1")) {
					state.setText("未中奖");
					joinu.setText("参与：" + itemData.getMy_buy_count() + "次");
				} else {
					int num = Integer.parseInt(itemData.getLucky_num());
					int luckNum = num;
					state.setText("中奖" + "(" + "幸运号" + luckNum + ")");
					joinu.setText("参与：" + itemData.getBuyCount() + "次");
				}
			}
		}
	}

	// 设置回调
	public static void setOnListItemSelectListener(
			OnListItemSelectListener<JoinRecordEntity> onListItemSelectListener) {
		JoinRecordHolder.onlistItemSelect = onListItemSelectListener;
	}
}
